package com.kexio.security.domain.enums;

/**
 * 会话状态枚举
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
public enum SessionStatus {
    /**
     * 活跃状态
     */
    ACTIVE(1, "活跃"),
    
    /**
     * 过期状态
     */
    EXPIRED(2, "过期"),
    
    /**
     * 已注销状态
     */
    LOGGED_OUT(3, "已注销"),
    
    /**
     * 被踢出状态
     */
    KICKED_OUT(4, "被踢出"),
    
    /**
     * 异常终止状态
     */
    ABNORMAL(5, "异常终止");

    private final Integer code;
    private final String description;

    SessionStatus(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    public Integer getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    /**
     * 根据编码获取状态
     * @param code 状态编码
     * @return 状态枚举
     */
    public static SessionStatus fromCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (SessionStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("无效的会话状态编码: " + code);
    }

    /**
     * 检查是否为活跃状态
     * @return 是否活跃
     */
    public boolean isActive() {
        return this == ACTIVE;
    }

    /**
     * 检查是否为已结束状态
     * @return 是否已结束
     */
    public boolean isEnded() {
        return this == EXPIRED || this == LOGGED_OUT || this == KICKED_OUT || this == ABNORMAL;
    }

    /**
     * 检查是否可以刷新
     * @return 是否可刷新
     */
    public boolean canRefresh() {
        return this == ACTIVE;
    }
}